<?php

/**
 * EmsServiceButton form.
 *
 * @package    ems1
 * @subpackage form
 * @author     Jacobo Martínez
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EmsServiceButtonForm extends BaseEmsServiceButtonForm
{
    public function configure()
    {
        unset($this['created_at'],$this['updated_at'], $this['portal_id'],
            $this['type_id'],$this['is_enabled']
        );

        $this->setWidget('thumb_small', new sfWidgetFormInputHidden());

        $this->setValidator('thumb_small',new sfValidatorString(array('required' => false)));

        $this->setWidget('thumb_small_type', new sfWidgetFormInputHidden());

        $this->setValidator('thumb_small_type',new sfValidatorString(array('max_length' => 100, 'required' => false)));

        $this->setWidget('thumb_small_size', new sfWidgetFormInputHidden());

        $this->setValidator('thumb_small_size',new sfValidatorInteger(array('required' => false)));

        $this->setWidget('thumb_small_tmp', new sfWidgetFormInputFile());

        $this->widgetSchema->setHelp('thumb_small_tmp','Debe cargar un archivo de formato .jpg y de dimensiones 25x25 pixeles.');

        $this->setValidator('thumb_small_tmp',new sfValidatorString(array('required' => false)));

        $this->setWidget('thumb_big', new sfWidgetFormInputHidden());

        $this->setValidator('thumb_big',new sfValidatorString(array('required' => false)));

        $this->setWidget('thumb_big_type', new sfWidgetFormInputHidden());

        $this->setValidator('thumb_big_type',new sfValidatorString(array('max_length' => 100, 'required' => false)));

        $this->setWidget('thumb_big_size', new sfWidgetFormInputHidden());

        $this->setValidator('thumb_big_size',new sfValidatorInteger(array('required' => false)));

        $this->setWidget('thumb_big_tmp', new sfWidgetFormInputFile());

        $this->widgetSchema->setHelp('thumb_big_tmp','Debe cargar un archivo de formato .jpg y de dimensiones 50x50 pixeles.');

        $this->setValidator('thumb_big_tmp',new sfValidatorString(array('required' => false)));

        $type = $this->getObject()->getType();

        if($type->getUsesMessage()){
            if($type->getType() == 'Email'){
                $this->widgetSchema['message'] = new sfWidgetFormTextareaTinyMCE(array(
                    'config' => 'skin : "o2k7",theme_advanced_disable: "anchor,cleanup,help"',
                    ));
            }
            
            $this->setValidator('message', new sfValidatorString(array(
                        'required' => true
                    ), array(
                        'required' => 'El mensaje no puede ser vacio.'
                    )));

            if($type->getMsgMaxLength() > 0){
                $this->validatorSchema['message']->setOption('max_length', $type->getMsgMaxLength());
                $this->validatorSchema['message']->addMessage('max_length','El nombre "%value%" excede el limite de caracteres (máximo %max_length% caracteres).');
            }
        } else {
            unset ($this['message']);
        }

        $this->getWidgetSchema()->setLabels(array(
            'thumb_small_tmp' => 'Icono pequeño',
            'thumb_big_tmp'   => 'Icono grande',
            'message'         => 'Mensaje',
            'published'       => 'Publicar'
        ));
    }

    protected function doUpdateObject($values)
    {
        $thumb_types = array('small', 'big');
        foreach ($thumb_types as $key => $type) {
            if(strcasecmp($values['thumb_'.$type],'') != 0){
                $path_parts_file = pathinfo($values['thumb_'.$type]);

                $folder = sfConfig::get('sf_upload_dir').'/portal_'.$this->getOption('portal_id');

                $actual = ($type == 'small' ? $this->getObject()->getThumbSmall() : $this->getObject()->getThumbBig());

                if(!$this->getObject()->exists() || strcasecmp($path_parts_file['basename'], $actual) != 0){
                   $values['thumb_'.$type] = $this->saveUploadedFile(
                                    $path_parts_file['basename'],
                                    $values['thumb_'.$type.'_type'],
                                    $values['thumb_'.$type],
                                    $values['thumb_'.$type.'_size'],
                                    $folder.'/images/'
                                );
                }
            }
        }

        unset(
            $values['thumb_small_type'], $values['thumb_small_size'], $values['thumb_small_tmp'],
            $values['thumb_big_type'], $values['thumb_big_size'], $values['thumb_big_tmp']
        );

        parent::doUpdateObject($values);
    }

    protected function saveUploadedFile($originalName, $type, $tempName, $size, $path) {

        $validated_file = new sfValidatedFile($originalName, $type, $tempName, $size, $path);

        return $validated_file->save();
    }
}